#ifndef HTTP_CONNECTION_H
#define HTTP_CONNECTION_H


#include <arpa/inet.h>
#include <sys/uio.h>
#include <cerrno>
#include <atomic>

#include "http_request.h"
#include "http_response.h"
#include "../lock/locker.h"

class HttpConnection {
public:
    explicit HttpConnection(uint64_t read_buf_size = 2048, uint64_t write_buf_size=2048);
    ~HttpConnection();
    void init(int sockfd, const sockaddr_in &addr_in);

    bool read_data();
    bool write_data(int *error);
    bool process();
    void close_conn();

    int get_fd() const;
    struct sockaddr_in get_sockaddr() const;
    const char* get_ip() const;
    int get_port() const;
    bool is_keepalive();

public:
    static std::atomic<int> m_clients_cnt;
    static const char* m_src_dir;
    static bool is_ET_mode;

public:
//private:
    void reset();
    int m_client_fd{};
    sockaddr_in m_client_addr{};

    HttpRequest m_Request;
    HttpResponse m_Response;

    int m_iov_cnt{};
    struct iovec m_iov[2]{};
    uint64_t m_bytes_to_send;
    uint64_t m_bytes_have_sent;

    bool m_is_close;
    // Locker m_locker;
};


#endif //HTTP_CONNECTION_H
